package www;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class windowsmess {
   public static void main(String args[]) {
      WindowButton win=new WindowButton("随机生成一个数字");
   }
}
class WindowButton extends JFrame implements ActionListener {
   int number;
   private int time = 30;
   JLabel hintLabel;
   JLabel baocuo;
   JTextField inputGuess;
   private JLabel label = new JLabel();
   JButton buttonGetNumber,buttonEnter;   
   WindowButton(String s) {
      super(s);
      addWindowListener( new WindowAdapter(){ //�������ʵ�����Ӵ����¼�
                            public void windowClosing(WindowEvent e) {
                                dispose();
                            }
                         } 
                       );  
      setLayout(new FlowLayout());
      buttonGetNumber=new JButton("随机生成一个数字");
      add(buttonGetNumber);
      hintLabel=new JLabel("请输入你的猜测",JLabel.CENTER);
      hintLabel.setBackground(Color.cyan);
      inputGuess=new JTextField("0",10); 
      add(hintLabel);
      add(inputGuess);
      buttonEnter=new JButton("确定"); 
      add(buttonEnter); 
      buttonEnter.addActionListener(this);
      buttonGetNumber.addActionListener(this);
      setBounds(100,100,150,150);
      setVisible(true);
      validate();
      add(label);
     	setVisible(true);
      	}
   public class MyThread extends Thread 
      	{	 
      	public void run() {
      		String a="答对了";
      	while (time > 0&&  !a.equals(hintLabel.getText()))
      	{
      	time--;
      	label.setText(time + "");
      	try
      	{
      	Thread.sleep(1000);
      	}
      	catch (Exception e)
      	{
      	e.printStackTrace();
     	}
      	}
   //  	WindowButton.this.dispose();
      	}
      	}
    
    public void actionPerformed(ActionEvent e) {
       
    	if(e.getSource()==buttonGetNumber) {
            number=(int)(Math.random()*100)+1;
            hintLabel.setText("请输入你的猜测：");
            Thread t=new MyThread();
      	  t.start();
         }

         else if(e.getSource()==buttonEnter&& time>0) {
            int guess=0;
            try {  guess=Integer.parseInt(inputGuess.getText());
                   if(guess==number) { 
                	   hintLabel.setText("答对了");
                	   Thread t=new MyThread();
                	   t.interrupt();
                      JOptionPane.showMessageDialog(this,"答对了","消息对话框",
                              JOptionPane.WARNING_MESSAGE);                
                   }
                   else if(guess>number) {
                      hintLabel.setText("猜大了");
                      inputGuess.setText(null); 
                   }
                   else if(guess<number) {
                       hintLabel.setText("猜小了");
                       inputGuess.setText(null); 
                   }
               }
             catch(NumberFormatException event) {
                   hintLabel.setText("输入正确格式");
             } 
         }
         else {
        	 Thread t=new MyThread();
        	  t.stop();
        	 JOptionPane.showMessageDialog(this,"超时","消息对话框",
                     JOptionPane.WARNING_MESSAGE);
        	
    } 
        
         
    }
}